﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils.Extend
{
    public static class ObservableExtension
    {
        public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable<T>
        {
            List<T> sortedList = collection.OrderBy(x => x).ToList();//这里用降序
            for (int i = 0; i < sortedList.Count(); i++)
            {
                collection.Move(collection.IndexOf(sortedList[i]), i);
            }
        }

        public static void Sort<T, TKey>(this ObservableCollection<T> collection, Func<T, TKey> keySelector) where T : IComparable<T>
        {
            List<T> sortedList = collection.OrderBy(keySelector).ToList();//这里用降序
            for (int i = 0; i < sortedList.Count(); i++)
            {
                collection.Move(collection.IndexOf(sortedList[i]), i);
            }
        }

        //public static void Sort<T, TKey>(this List<T> collection, Func<T, TKey> keySelector) where T : IComparable<T>
        //{
        //    List<T> sortedList = collection.OrderBy(keySelector).ToList();//这里用降序
        //    for (int i = 0; i < sortedList.Count(); i++)
        //    {
        //        collection.Move(collection.IndexOf(sortedList[i]), i);
        //    }
        //}
    }
}
